#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from datetime import datetime
from app import db

'''
from flask_sqlalchemy import SQLAlchemy
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql+pymysql://root:root@127.0.0.1:3306/movie'
db = SQLAlchemy(app) 
'''


class User(db.Model):  # 定义会员模型
    __tablename__ = "user"
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(80), unique=True)
    pwd = db.Column(db.String(100))
    email = db.Column(db.String(120), unique=True)
    phone = db.Column(db.String(11), unique=True)
    status = db.Column(db.SmallInteger, default=1)
    info = db.Column(db.Text)
    avatar = db.Column(db.String(255))
    addtime = db.Column(db.DateTime, index=True, default=datetime.now)  # 注册时间
    uuid = db.Column(db.String(255), unique=True)
    userlogs = db.relationship('Userlog', backref='user')  # 会员日志关系关联
    comments = db.relationship('Comment', backref='user')  # 电影评论关系关联
    moviecols = db.relationship('Moviecol', backref='user')  # 电影收藏关系关联

    def __repr__(self):  # 返回类型
        return '<User %r>' % self.name

    def check_pwd(self, pwd):
        from werkzeug.security import check_password_hash
        return check_password_hash(self.pwd, pwd)


class Userlog(db.Model):  # 定义会员登录日志
    __tablename__ = "userlog"
    id = db.Column(db.Integer, primary_key=True)
    user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
    ip = db.Column(db.String(100))
    logintime = db.Column(db.DateTime, index=True, default=datetime.now)  # 登录时间

    def __repr__(self):
        return '<Userlog %r>' % self.id


class Tag(db.Model):  # 标签数据模型
    __tablename__ = "tag"
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(80), unique=True)
    addtime = db.Column(db.DateTime, index=True, default=datetime.now)  # 添加时间
    movies = db.relationship("Movie", backref='tag')  # 电影关系关联

    def __repr__(self):
        return '<Tag %r>' % self.name


class Movie(db.Model):  # 标签数据模型
    __tablename__ = "movie"
    id = db.Column(db.Integer, primary_key=True)
    title = db.Column(db.String(255), unique=True)
    url = db.Column(db.String(255), unique=True)
    info = db.Column(db.Text)
    logo = db.Column(db.String(255), unique=True)
    star = db.Column(db.SmallInteger)
    playnum = db.Column(db.BigInteger)
    commentnum = db.Column(db.BigInteger)
    tag_id = db.Column(db.Integer, db.ForeignKey('tag.id'))  # 所属标签
    area = db.Column(db.String(255))  # 上映地区
    release_time = db.Column(db.Date)  # 上映时间
    length = db.Column(db.String(100))  # 播放电影长度
    addtime = db.Column(db.DateTime, index=True, default=datetime.now)  # 添加时间
    comments = db.relationship('Comment', backref='movie')  # 电影评论关系关联
    moviecols = db.relationship('Moviecol', backref='movie')  # 电影收藏关系关联

    def __repr__(self):
        return '<Movie %r>' % self.title


class Preview(db.Model):  # 上映预告
    __tablename__ = 'preview'
    id = db.Column(db.Integer, primary_key=True)
    title = db.Column(db.String(255), unique=True)
    logo = db.Column(db.String(255), unique=True)
    addtime = db.Column(db.DateTime, index=True, default=datetime.now)

    def __repr__(self):
        return '<Preview %r>' % self.title


class Comment(db.Model):  # 电影评论
    __tablename__ = 'comment'
    id = db.Column(db.Integer, primary_key=True)
    content = db.Column(db.Text)
    movie_id = db.Column(db.Integer, db.ForeignKey('movie.id'))
    user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
    addtime = db.Column(db.DateTime, index=True, default=datetime.now)

    def __repr__(self):
        return '<Comment %r>' % self.id


class Moviecol(db.Model):  # 电影收藏
    __tablename__ = 'moviecol'
    id = db.Column(db.Integer, primary_key=True)
    movie_id = db.Column(db.Integer, db.ForeignKey('movie.id'))
    user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
    addtime = db.Column(db.DateTime, index=True, default=datetime.now)

    def __repr__(self):
        return '<Moviecol %r>' % self.id


class Auth(db.Model):  # 权限
    __tablename__ = 'auth'
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(100), unique=True)  # 权限名称
    url = db.Column(db.String(255), unique=True)  # 权限地址
    addtime = db.Column(db.DateTime, index=True, default=datetime.now)  # 添加时间

    def __repr__(self):
        return '<Auth %r>' % self.name


class Role(db.Model):  # 角色
    __tablename__ = 'role'
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(100), unique=True)  # 角色名称
    auths = db.Column(db.String(600))  # 权限列表
    addtime = db.Column(db.DateTime, index=True, default=datetime.now)  # 添加时间
    admins = db.relationship('Admin', backref='role')  # 管理员角色关系关联

    def __repr__(self):
        return '<Role %r>' % self.name


class Admin(db.Model):  # 管理员模型
    __tablename__ = "admin"
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(80), unique=True)
    pwd = db.Column(db.String(100))
    is_super = db.Column(db.SmallInteger, default=1)  # 是否为超级管理员, 0为超级管理员
    role_id = db.Column(db.Integer, db.ForeignKey('role.id'))  # 关联角色id
    addtime = db.Column(db.DateTime, index=True, default=datetime.now)  # 注册时间
    adminlogs = db.relationship('Adminlog', backref='admin')  # 管理员日志关系关联
    operatelogs = db.relationship('Operatelog', backref='admin')  # 操作日志关系关联

    def __repr__(self):
        return '<Admin %r>' % self.name

    def check_pwd(self, pwd):
        from werkzeug.security import check_password_hash
        return check_password_hash(self.pwd, pwd)  # 模型密码,传入密码


class Adminlog(db.Model):  # 管理员登录日志
    __tablename__ = "adminlog"
    id = db.Column(db.Integer, primary_key=True)
    admin_id = db.Column(db.Integer, db.ForeignKey('admin.id'))
    ip = db.Column(db.String(100))
    logintime = db.Column(db.DateTime, index=True, default=datetime.now)  # 登录时间

    def __repr__(self):
        return '<Adminlog %r>' % self.id


class Operatelog(db.Model):  # 管理员登录日志
    __tablename__ = "operatelog"
    id = db.Column(db.Integer, primary_key=True)
    admin_id = db.Column(db.Integer, db.ForeignKey('admin.id'))  # 关联管理员id
    ip = db.Column(db.String(100))
    reason = db.Column(db.String(600))  # 操作的原因
    logintime = db.Column(db.DateTime, index=True, default=datetime.now)  # 登录时间

    def __repr__(self):
        return '<Operatelog %r>' % self.id


''' 插入数据
if __name__ == "__main__":
    # db.create_all()
    
    role = Role(
        name="超级管理员",
        auths=""
    )
    db.session.add(role)
    db.session.commit()

    # 插入admin
    from werkzeug.security import generate_password_hash
    admin = Admin(
        name="aqie1",
        pwd=generate_password_hash("123"),
        is_super=0,
        role_id=1
    )
    db.session.add(admin)
    db.session.commit()
'''
